﻿using System;
using System.Web;
using System.Web.Caching;

using SimpleForum.Data.Services;

namespace SimpleForum.Classes.Achievements
{
    public abstract class AchievementWorker
    {
        protected AchievementWorker()
        {
            //start cycling on initialization
            Insert();
        }

        //override to provide specific achievement logic
        protected abstract void AwardAchievement();

        //how long to wait between iterations
        protected abstract TimeSpan Interval { get; }

        private void Callback(string key, object value, CacheItemRemovedReason reason)
        {
            if (reason == CacheItemRemovedReason.Expired)
            {
                this.AwardAchievement();
                this.Insert();
            }
        }

        private void Insert()
        {
            HttpRuntime.Cache.Add
            (
                this.GetType().ToString(),
                this,
                null,
                Cache.NoAbsoluteExpiration,
                this.Interval,
                CacheItemPriority.Normal,
                this.Callback
            );
        }
    }
}